package com.tengju.bff.interfaces.covercharge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tengju.bff.interfaces.covercharge.constant.CustTaskQueueStatusEnum;
import com.tengju.bff.interfaces.covercharge.entity.po.CustTaskQueue;
import com.tengju.bff.interfaces.covercharge.mapper.CustTaskQueueMapper;
import com.tengju.bff.interfaces.covercharge.service.ICustTaskQueueService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 账户-客户业务自定义任务队列表 服务实现类
 * </p>
 *
 * @author qumengmeng
 * @since 2020-09-23
 */
@Service
public class CustTaskQueueServiceImpl extends ServiceImpl<CustTaskQueueMapper, CustTaskQueue> implements ICustTaskQueueService {

    @Override
    public Integer notRunningOutOrder(String cmd) {
        QueryWrapper<CustTaskQueue> wrapper = new QueryWrapper<CustTaskQueue>();
        wrapper.eq("status", CustTaskQueueStatusEnum.IS_NOT_RUNN.getCode());
        wrapper.eq("cmd", cmd);

        return baseMapper.selectCount(wrapper);
    }

    @Override
    public CustTaskQueue getQueueIsNotRunning(String cmd) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        CustTaskQueue custTaskQueue = baseMapper.getQueueFirst(cmd, CustTaskQueueStatusEnum.IS_NOT_RUNN.getCode(), dtf2.format(LocalDateTime.now()));
        if(custTaskQueue != null) {
            custTaskQueue.setStatus(CustTaskQueueStatusEnum.EXECUTION_OF.getCode());
            custTaskQueue.setUpdateTime(LocalDateTime.now());
            this.updateById(custTaskQueue);
        }

        return custTaskQueue;
    }
}
